package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 根据环信id查询用户信息
     *
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity userinfo(String huanxinId) {
        UserInfoVo vo = messagesService.findUserInfoByHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 陌生人添加好友
     *
     * @param map
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        Long friendId = Long.valueOf(map.get("userId").toString());
        messagesService.contacts(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友列表
     *
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity contacts(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String keyword) {
        PageResult pr = messagesService.findFriends(page, pagesize, keyword);
        return ResponseEntity.ok(pr);
    }

    @GetMapping({"/likes", "/loves", "/comments"})
    public ResponseEntity likes(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        String requestURI = request.getRequestURI();
        PageResult pr;
        if (requestURI.contains("likes")) {
            pr = messagesService.findLikesOrLoves(page, pagesize, CommentType.LIKE);
        } else if (requestURI.contains("comments")) {
            pr = messagesService.findLikesOrLoves(page, pagesize, CommentType.COMMENT);
        } else {
            pr = messagesService.findLikesOrLoves(page, pagesize, CommentType.LOVE);
        }
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/announcements")
    public ResponseEntity announcements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = messagesService.findAnnouncements(page, pagesize);
        return ResponseEntity.ok(pr);
    }
}